package com.br4mmie.asciistory.ui;

import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.Keybindings;

/**
 * 
 * Window base class, all windows inherit from this class. holds the basic functions for opening, drawing and closing
 *
 */
public class Window {
	
	protected int locX, locY; //the location of the windows' interior, this is what the inherited classes work with
	private int realX, realY; //the real location of the window, used to make sure there is room for a border
	protected int width, height; //the width and height of the windows' interior
	private int realWidth, realHeight;//the actual width and height of the window
	protected CharImage image;//the windows' image
	protected boolean isActive;//used to check if this window is the currently active window.
	protected boolean canBeClosed;//used to check if the window can be closed by pressing X, will show X ni upperleft corner if true
	
	public boolean isClosed;//used to check if the window is closed. if set true the window handler will remove the window
	
	/**
	 *  basic constructor 
	 * @param x the desired x coordinate 
	 * @param y the desired y coordinate
	 * @param w the desired width
	 * @param h the desired height
	 */
	public Window(int x, int y, int w, int h)
	{
		realX = x;
		realY = y;
		realWidth = w;
		realHeight = h;
		locX = x+1; //leave room for border
		locY = y+1; //leave room for border
		width = w-2; //leave room for border
		height = h-2; //leave room for border
		image = new CharImage(realWidth, realHeight);
		isActive = false;
		isClosed = false;
		canBeClosed = true;
		ASCIIStoryActivity.getGameLoop().addWindow(this); //add this window to the windowhandlers' list
	}
	
	/**
	 *  Function to resize the window
	 * @param x the desired x coordinate 
	 * @param y the desired y coordinate
	 * @param w the desired width
	 * @param h the desired height
	 */
	public void resize(int x, int y, int w, int h)
	{
		if(w < 3 || h < 3)System.out.println("ERROR: Window is too small");
		realX = x;
		realY = y;
		realWidth = w;
		realHeight = h;
		locX = x+1;
		locY = y+1;
		width = w-2;
		height = h-2;
		image = new CharImage(realWidth, realHeight);
	}
	
	/**
	 * draws the window to the passed CharImage
	 * @param target CharImage to draw to
	 */
	public void draw(CharImage target)
	{
		drawBording();
		image.draw(target, realX, realY);
	}
	
	/**
	 *clears the window to ' '
	 */
	public void clear()
	{
		image.fill(' ');
	}
	
	/**
	 * Draws the windows' bording
	 */
	private void drawBording()
	{
		image.rect('+', 0, 0, realWidth-1, realHeight-1);
		image.putChar('#', 0, 0);
		image.putChar('#', 0, realHeight-1);
		image.putChar('#', realWidth-1, realHeight-1);
		if(canBeClosed)image.putChar('X', realWidth-1, 0);
		else image.putChar('#', realWidth-1, 0);
	}
	
	/**
	 * Handles the closing of the window, call when you want a window to close.
	 */
	public void close()
	{
		isActive = false;
		image = null;
		isClosed = true;
		
		
	}
	
	/**
	 * Handles Keyboard input
	 */
	protected void getInput()
	{
		if(!isActive)return;
		boolean keypressed = false;
				
		if(canBeClosed && (Keybindings.CLOSE.pressed()))
		{
			keypressed = true;
			close();
		}
	
		
		if(keypressed)
		{
			try{
				Thread.sleep(500);
			}
			catch (Exception ex) {}
		}
	}
	
	/**
	 * Pauses the window until an Enter is given.
	 */
	public void waitForEnter()
	{
		while(!(Keybindings.INTERACT.pressed()))
		{
			if(Keybindings.CLOSE.pressed())
			{
				close();
				break;
			}
			
		}
	}
	
	/**
	 * Moves the window to the given x and y coordinate
	 * @param x
	 * @param y
	 */
	public void moveTo(int x,int y)
	{
		realX = x;
		realY = y;
	}
	
	/**
	 * Basic getters and setters
	 * basic functions
	 */
	
	public int getX()
	{
		return realX;
	}
	
	public int getY()
	{
		return realY;
	}
	
	public int getWidth()
	{
		return width;
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public void activate()
	{
		isActive = true;
	}
	
	public void deactivate()
	{
		isActive = false;
	}
	
	public boolean active()
	{
		return isActive;
	}
	
	public void onLoop()
	{
		
	}
	
	public void handleEvents()
	{
		getInput();
	}
}
